﻿using UML_Creator.Model;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace UML_Creator.Command
{
    // Bruges til at tilføje punkt til punkt samlingen.
   
    public class EditNodeCommand : IUndoRedoCommand
    {
        private Diagram node;
        private int diagramType;
        private IList<string> changeList = new List<string>();

        public EditNodeCommand(Diagram _node, int _diagramType) {node = _node; diagramType = _diagramType; }

        public void Execute()
        {
            if (diagramType == (int)UML_Creator.ViewModel.EnumDiagramType.ActivityDiagram) node = new ActivityDiagram();
            else if (diagramType == (int)UML_Creator.ViewModel.EnumDiagramType.ClassDiagram)
            {
                //Get original data and keep it for undoing
                ClassDiagram editNode = node as ClassDiagram;
                string originalClassName = editNode.ClassName;
                string originalClassAttributes = editNode.ClassAttributes;
                string originalClassMethods = editNode.ClassMethods;
                changeList.Add(originalClassName);
                changeList.Add(originalClassAttributes);
                changeList.Add(originalClassMethods);

                var window = new UML_Creator.View.EditClassDiagramClass();
                window.DataContext = node;

                if (window.ShowDialog() == false)
                {
                    editNode.ClassName = originalClassName;
                    editNode.ClassAttributes = originalClassAttributes;
                    editNode.ClassMethods = originalClassMethods;
                }
            }
            else if (diagramType == (int)UML_Creator.ViewModel.EnumDiagramType.InterfaceDiagram)
            {
                InterfaceDiagram editNode = node as InterfaceDiagram;
                string originalInterfaceName = editNode.InterfaceName;
                string originalInterfaceMethods = editNode.InterfaceMethods;
                changeList.Add(originalInterfaceName);
                changeList.Add(originalInterfaceMethods);

                var window = new UML_Creator.View.EditClassDiagramInterfaceWindow();
                window.DataContext = node;

                if (window.ShowDialog() == false)
                {
                    editNode.InterfaceName = originalInterfaceName;
                    editNode.InterfaceMethods = originalInterfaceMethods;
                }
            }


        }

        public void UnExecute()
        {
            if (diagramType == (int)UML_Creator.ViewModel.EnumDiagramType.ActivityDiagram) node = new ActivityDiagram();
            else if (diagramType == (int)UML_Creator.ViewModel.EnumDiagramType.ClassDiagram)
            {
                //Get original data
                ClassDiagram editNode = node as ClassDiagram;

                //Get post-edited strings for redos
                string editedClassName = editNode.ClassName;
                string editedClassAttributes = editNode.ClassAttributes;
                string editedClassMethods = editNode.ClassMethods;

                //Set original strings
                editNode.ClassName = changeList.ElementAt(0);
                editNode.ClassAttributes = changeList.ElementAt(1);
                editNode.ClassMethods = changeList.ElementAt(2);
                changeList.Clear();

                //Save edited strings for redo
                changeList.Add(editedClassName);
                changeList.Add(editedClassAttributes);
                changeList.Add(editedClassMethods);
            }
            else if (diagramType == (int)UML_Creator.ViewModel.EnumDiagramType.InterfaceDiagram)
            {
                //Get original data
                InterfaceDiagram editNode = node as InterfaceDiagram;

                //Get post-edited strings for redos
                string editedInterfaceName = editNode.InterfaceName;
                string editedInterfaceMethods = editNode.InterfaceMethods;

                //Set original strings
                editNode.InterfaceName = changeList.ElementAt(0);
                editNode.InterfaceMethods = changeList.ElementAt(1);
                changeList.Clear();

                //Save edited strings for redo
                changeList.Add(editedInterfaceName);
                changeList.Add(editedInterfaceMethods);
            }
        }

        public void ReExecute()
        {
            if (diagramType == (int)UML_Creator.ViewModel.EnumDiagramType.ActivityDiagram) node = new ActivityDiagram();
            else if (diagramType == (int)UML_Creator.ViewModel.EnumDiagramType.ClassDiagram)
            {
                //Get original data
                ClassDiagram editNode = node as ClassDiagram;

                editNode.ClassName = changeList.ElementAt(0);
                editNode.ClassAttributes = changeList.ElementAt(1);
                editNode.ClassMethods = changeList.ElementAt(2);
                changeList.Clear();
            }
            else if (diagramType == (int)UML_Creator.ViewModel.EnumDiagramType.InterfaceDiagram)
            {
                //Get original data
                InterfaceDiagram editNode = node as InterfaceDiagram;

                editNode.InterfaceName = changeList.ElementAt(0);
                editNode.InterfaceMethods = changeList.ElementAt(1);
                changeList.Clear();
            }
        }
    }
}
